import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

// 身份验证接口
interface Authentication {
    boolean authenticate(String username, String password);
}
// 日志记录接口
interface Logging {
    void logQuery(String userInfo, String queryDetails);
}
// 商务信息查询接口
interface BusinessInfoQuery {
    String queryBusinessInfo(String queryDetails);
}
// 简单的用户名密码验证实现
class SimpleAuthentication implements Authentication {
    private String validUsername = "admin";
    private String validPassword = "password";

    @Override
    public boolean authenticate(String username, String password) {
        return validUsername.equals(username) && validPassword.equals(password);
    }
}

// 简单的日志记录实现，将日志输出到控制台
class SimpleLogging implements Logging {
    @Override
    public void logQuery(String userInfo, String queryDetails) {
        System.out.println("User: " + userInfo + ", Query: " + queryDetails + ", Timestamp: " + System.currentTimeMillis());
    }
}

// 商务信息查询实现，这里只是模拟返回一些信息
class BusinessInfoQueryImpl implements BusinessInfoQuery {
    @Override
    public String queryBusinessInfo(String queryDetails) {
        // 这里应该连接数据库或调用API来查询信息，但为了简化，我们直接返回模拟数据
        return "Business Info for " + queryDetails;
    }
}


class BusinessInfoQueryProxy implements BusinessInfoQuery{
    private BusinessInfoQuery businessInfoQuery = null;

    public BusinessInfoQueryProxy(BusinessInfoQuery businessInfoQuery) {
        this.businessInfoQuery = businessInfoQuery;
    }

    @Override
    public String queryBusinessInfo(String queryDetails) {

        return this.businessInfoQuery.queryBusinessInfo(queryDetails);
    }
}





public class ProxyDemo {
    public static void main(String[] args) {
        BusinessInfoQuery businessInfoQuery = new BusinessInfoQueryImpl();
        BusinessInfoQuery proxy = (BusinessInfoQuery) Proxy.newProxyInstance(businessInfoQuery.getClass().getClassLoader(), businessInfoQuery.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("before");

                return method.invoke(businessInfoQuery,args);
            }
        });
        System.out.println(proxy.queryBusinessInfo("world"));

    }
}
